﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Windows.Forms;

namespace Streambolics.Gui
{
    public class SubMenu : Component
    {
        private ToolStripMenuItem _ParentItem;
        private ContextMenuStrip _ParentMenu;

        protected void Add (ToolStripItem aItem)
        {
            if (_ParentItem != null)
            {
                _ParentItem.DropDownItems.Add (aItem);
            }
            else if (_ParentMenu != null)
            {
                _ParentMenu.Items.Add (aItem);
            }
        }

        public ToolStripMenuItem ParentItem
        {
            get
            {
                return _ParentItem;
            }
            set
            {
                if (_ParentItem != value)
                {
                    _ParentItem = value;
                    if (value != null)
                    {
                        _ParentMenu = null;
                        _ParentItem.DropDownOpening += delegate (object sender, EventArgs e)
                        {
                            DoRedraw ();
                        };
                    }
                    DoBuildMenu ();
                }
            }
        }

        public ContextMenuStrip ParentMenu
        {
            get
            {
                return _ParentMenu;
            }
            set
            {
                if (_ParentMenu != value)
                {
                    _ParentMenu = value;
                    if (value != null)
                    {
                        _ParentItem = null;
                        _ParentMenu.Opening += delegate (object sender, CancelEventArgs e)
                        {
                            DoRedraw ();
                        };
                    }
                    DoBuildMenu ();
                }
            }
        }

        protected void DoBuildMenu ()
        {
            if (!DesignMode)
            {
                if (_ParentMenu != null || _ParentItem != null)
                {
                    OnBuildMenu (EventArgs.Empty);
                }
            }
        }

        protected virtual void OnBuildMenu (EventArgs e)
        {
        }

        protected void DoRedraw ()
        {
            OnRedraw (EventArgs.Empty);
        }

        protected virtual void OnRedraw (EventArgs e)
        {
        }
    }
}
